﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace QLNSClient
{
    public partial class uc_qdKhenthuong : UserControl
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        //int flag = 0;
        public uc_qdKhenthuong()
        {
            InitializeComponent();
        }

        private void labelControl2_Click(object sender, EventArgs e)
        {

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            lke_DVHC.Enabled = false;
            lke_MaCB.Enabled = false;
            lke_maKT.Enabled = false;
            txt_QuyetDinh.Enabled = false;
            de_NgayKy.Enabled = false;
            txtNoiDung.Enabled = false;
            btn_themAnh.Enabled = false;
            btn_xoaAnh.Enabled = false;

            lke_DVHC.Properties.DataSource = sv.getDMDV();

            lke_MaCB.Properties.DataSource = sv.getCB();

            lke_maKT.Properties.DataSource = sv.getKT();

            gv_dsKT.DataSource = sv.getCBKT();
        }

        private void uc_qdKhenthuong_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            lke_DVHC.Enabled = true;
            lke_MaCB.Enabled = true;
            lke_maKT.Enabled = true;
            txt_QuyetDinh.Enabled = true;
            de_NgayKy.Enabled = true;
            txtNoiDung.Enabled = true;
            
            btn_themAnh.Enabled = true;
            btn_xoaAnh.Enabled = true;

            lke_MaCB.EditValue = "";
            lke_DVHC.EditValue = "";
            lke_maKT.EditValue = null;
            txtNoiDung.Text = "";
            txt_QuyetDinh.Text = "";
            de_NgayKy.Text = "";
            pte_AnhKT.Image = null;

            gv_dsKT.DataSource = sv.getCBKT();
        }



        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {

            demsua = 1;
            
            btn_themAnh.Enabled = true;
            btn_xoaAnh.Enabled = true;
            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;



            txt_QuyetDinh.Enabled = true;
            de_NgayKy.Enabled = true;
            txtNoiDung.Enabled = true;
            btn_themAnh.Enabled = true;
            btn_xoaAnh.Enabled = true;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
            
            DialogResult dr = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này?!", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteCBKT(makt, macb);
                LoadData();
                MessageBox.Show("Xóa thành công!");
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Luu.Enabled = false;
            btn_Them.Enabled = true;
            
            

            try
            {
                string tempCB = lke_MaCB.EditValue.ToString();
                string tempKT = lke_maKT.EditValue.ToString();
                if (tempCB == "" || tempKT == "")
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
                }
                else
                {
                    string tempNoiDung = txtNoiDung.Text;
                    string tempQuyetDinh = txt_QuyetDinh.Text;
                    DateTime tempNgayKy = de_NgayKy.DateTime;
                    QLNSServiceReference.CANBO_KHENTHUONG cbkt = new QLNSServiceReference.CANBO_KHENTHUONG();
                    cbkt.Ma_CB = tempCB;
                    cbkt.Ma_KT = Int32.Parse(tempKT);
                    cbkt.NoiDung = tempNoiDung;
                    cbkt.QuyetDinh = tempQuyetDinh;
                    cbkt.NgayKy = tempNgayKy;
                    if (tempPath != null)
                    {
                        cbkt.FileQuyetDinh = ReadImageFromFile(tempPath);
                    }
                    else
                    {
                        cbkt.FileQuyetDinh = new Byte[0];
                    }
                    int kq;
                    kq = sv.addCBKT(cbkt);
                    if (kq != -1)
                    {
                        MessageBox.Show("Lưu thành công!");
                        gv_dsKT.DataSource = sv.getCBKTtheoDV(tempDVHC);
                        //LoadData();
                    }
                    else
                    {
                        if (demsua == 0)
                        {
                            DialogResult dr = MessageBox.Show("Thông tin cán bộ đã tồn tại!\n Bạn muốn sửa thông tin cán bộ này?", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                            if (dr == DialogResult.OK)
                            {
                                sv.updateCBKT(cbkt);

                                LoadData();
                                btn_Huy.Enabled = true;
                            }
                        }
                        else
                        {
                            sv.updateCBKT(cbkt);
                            MessageBox.Show("Sửa thành công!");
                            LoadData();
                            demsua = 0;
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
            }
        }


        string tempDVHC;
        private void lke_DVHC_EditValueChanged(object sender, EventArgs e)
        {
            tempDVHC = lke_DVHC.EditValue.ToString();
            lke_MaCB.Properties.DataSource = sv.getCBtheoDV(tempDVHC);
            gv_dsKT.DataSource = sv.getCBKTtheoDV(tempDVHC);

        }

        private void lke_MaKT_EditValueChanged(object sender, EventArgs e)
        {

        }


        //-----------ham ma hoa anh sang ma nhi phan--------------------

        public static Byte[] ReadImageFromFile(string ImagePath)
        {
            Byte[] ImageData = null;  //khai bao mang ImageData kieu byte luu tru anh
            FileInfo fi = new FileInfo(ImagePath); //      khai bao bien fi kieu FileInfo de doc thong tin ImagePath
            long NumberOfBytes = fi.Length; // khai bao bien NumberOfBytes kieu Long de luu do dai cua bien fi
            FileStream fs = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);         // bien br doc bien fs
            ImageData = br.ReadBytes((int)NumberOfBytes);
            return ImageData;
        }


        // ------------------------------------------dua du lieu len gridcontrol............................................
        public static string macb = "";
        public static int makt;
        public static Byte[] byteBLOBData;
        //string tempAnh;
        //string g;
        private void gv_dsKT_Click(object sender, EventArgs e)
        {

            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_themAnh.Enabled = false;
            btn_xoaAnh.Enabled = false;


            lke_MaCB.Enabled = false;
            lke_DVHC.Enabled = false;
            lke_maKT.Enabled = false;
            txt_QuyetDinh.Enabled = false;
            de_NgayKy.Enabled = false;
            txtNoiDung.Enabled = false;
            btn_themAnh.Enabled = false;
            btn_xoaAnh.Enabled = false;
            
            try
            {
                
                lke_MaCB.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                lke_maKT.EditValue = Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]));
                txtNoiDung.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                txt_QuyetDinh.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);
                de_NgayKy.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[4]));

                
                macb = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                makt = Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]));


                //----------------------lay ten don vi----------------------------

                List<QLNSServiceReference.DANHMUC_DONVI> listTenDVtheoMaCB = new List<QLNSServiceReference.DANHMUC_DONVI>(sv.getTenDVtheoMaCB(macb));
                string a = listTenDVtheoMaCB[0].Ma_DV;
                lke_DVHC.EditValue = a;

                
                 //-----------------------------------------------------------

                DataTable dt_anh = new DataTable();
                sqlHelper.Fill(dt_anh, "select FileQuyetDinh from CANBO_KHENTHUONG where Ma_CB='"+macb+"' and Ma_KT='"+makt+"'");

                byteBLOBData = new Byte[0];
                byteBLOBData = (Byte[])(dt_anh.Rows[0][0]);
                if (byteBLOBData.Length != 0)
                {
                    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    pte_AnhKT.Image = Image.FromStream(stmBLOBData);
                    
                    
                }
                else
                {
                    pte_AnhKT.Image = null;
                  }

            }
            catch { }
        }


        string tempPath;
        private void btn_themAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "JPG|*.JPG|PNG|*.PNG|BITMAP|*.BMP";
            if (op.ShowDialog() == DialogResult.OK)
            {
                pte_AnhKT.Image = Image.FromFile(op.FileName);
                tempPath = op.FileName;
            }
        }

        private void btn_All_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            lke_MaCB.EditValue = "";
            lke_DVHC.EditValue = "";
            lke_maKT.EditValue = null;
            txtNoiDung.Text = "";
            txt_QuyetDinh.Text = "";
            de_NgayKy.Text = "";
            pte_AnhKT.Image = null;

            lke_MaCB.Enabled = false;
            lke_DVHC.Enabled = false;
            lke_maKT.Enabled = false;
            txt_QuyetDinh.Enabled = false;
            de_NgayKy.Enabled = false;
            txtNoiDung.Enabled = false;
            btn_themAnh.Enabled = false;
            btn_xoaAnh.Enabled = false;


        }

        private void pte_AnhKT_ZoomPercentChanged(object sender, EventArgs e)
        {
          //  pte_AnhKT.ClientSize = new System.Drawing.Size(pte_AnhKT.Image.Width + 2, pte_AnhKT.Image.Height + 2);


        }

        private void pte_AnhKT_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void pte_AnhKT_Click(object sender, EventArgs e)
        {


            viewImgKT frm = new viewImgKT();
            frm.ShowDialog();

        }
        public static string BinaryToString(string data)
        {
            List<Byte> byteList = new List<Byte>();

            for (int i = 0; i < data.Length; i += 8)
            {
                byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
            }

            return Encoding.ASCII.GetString(byteList.ToArray());
        }
    }
}
